import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {BrowserModule} from '@angular/platform-browser';
import {HttpModule, Http, XHRBackend, BaseRequestOptions} from '@angular/http';
import {NgLayer} from 'ng2-layer';
import {NgModule, enableProdMode, Injectable, Provider}  from '@angular/core';

import {FormsModule}  from '@angular/forms';

import {MenuComp} from "app/MenuComp";
import {ContentComp} from "app/ContentComp";

import {DirectivesM} from "app/Directives";

import {AppComp} from "app/AppComp";
import {LoginComp} from "app/LoginComp";

import { appRoutes } from "app/AppRoute";
import { Notify } from "app/Service";

import {DataComp} from "app/Router/DataComp";
import {OrderComp} from "app/Router/OrderComp";
import {PayComp} from "app/Router/pay/PayComp";
import {BindWxComp} from "app/Router/pay/BindWxComp";

import {UserComp} from "app/Router/UserComp";
import {SettingComp} from "app/Router/SettingComp";
import {BoxComp} from "app/Router/BoxComp";
import {IndexSettingComp} from "app/Router/IndexSettingComp";

/**
 * 盒子设置
 */
import {BoxAccountComp}  from 'app/Router/boxSetting/BoxAccountComp';
import {BoxAddressComp}  from 'app/Router/boxSetting/BoxAddressComp';
import {BoxBaseComp}  from 'app/Router/boxSetting/BoxBaseComp';
import {BoxNoticeComp}  from 'app/Router/boxSetting/BoxNoticeComp';
import {BoxShipComp}  from 'app/Router/boxSetting/BoxShipComp';
import {BoxTimeComp}  from 'app/Router/boxSetting/BoxTimeComp';
import {BoxSettingIndexComp} from "app/Router/boxSetting/BoxSettingIndexComp";

/**
 * 盒子商品
 */
import {BoxCategoryComp}  from 'app/Router/boxProduct/BoxCategoryComp';
import {BoxProductComp}  from 'app/Router/boxProduct/BoxProductComp';
import {BoxProductIndexComp} from "app/Router/boxProduct/BoxProductIndexComp";

//enableProdMode();

@Injectable()
export class Ajax extends Http {
	constructor(backend: XHRBackend, private layer:NgLayer, private notify:Notify) {
		super(backend, new BaseRequestOptions());
		this._defaultOptions.headers.append("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
	}
	
	url:string = "http://api.txiaoyuan.cn/index/";

	request(url: string, options:RequestOptionsArgs): Observable <Response> {
		return super.request(url, options);
	}

	/**
	 * 
	 */
	get(options:RequestOptionsArgs): Promise {
		options.body = this._parseBody(options.body);
		options.search = this._parseBody(options.search);
		options.withCredentials = 'true';
		
		let ly = null;
		if(options.message){
			ly = this.layer.loading({message:"loading...", isModal:true});
		}

		if(options.url){
			options.url = this.url+options.url;
		} else {
			options.url = this.url+"t.php";
		}
		
		return new Promise((resolve, reject)=>{
			super.get(options.url, options).subscribe(res => {
				let data = res.json();
				
				if(data.code==200){
					resolve(data);
				} else {
					if(data.code==0){
						this.notify.emit(false);
					}
					
					this.layer.tip({
						message:data.message,
						align:"top"
					})
				}
				
				if(ly){
					ly.close();
				}
			});
		});
	}
	
	/**
	 * 
	 */
	post(options:RequestOptionsArgs): Promise {
		options.body = this._parseBody(options.body);
		options.search = this._parseBody(options.search);
		options.withCredentials = 'true';
		
		if(options.url){
			options.url = this.url+options.url;
		} else {
			options.url = this.url+"t.php";
		}
		
		let ly = null;
		if(options.message){
			ly = this.layer.loading({message:"loading...", isModal:true});
		}
		
		return new Promise((resolve, reject)=>{
			super.post(options.url, null, options).subscribe(res => {
				let data = res.json();
				
				if(data.code==200){
					resolve(data);
				} else {
					if(data.code==0){
						this.notify.emit(false);
					}
					
					this.layer.tip({
						message:data.message
					})
				}
				
				if(ly){
					ly.close();
				}
			});
		});
	}
	
	//参数解析
	_parseBody(obj:any):string{
		if(!obj || String(obj)=='[object File]' || obj instanceof FormData || typeof obj == "string") return obj;
		
		let query = '', name, value, fullSubName, subName, subValue, innerObj, i;
		
		for (name in obj) {
			value = obj[name];

			if (value instanceof Array) {
				for (i = 0; i < value.length; ++i) {
					subValue = value[i];
					if(subValue==0 || subValue){
						fullSubName = name + "[]";
						innerObj = {};
						innerObj[fullSubName] = subValue;
						query += this._parseBody(innerObj) + '&';
					}
				}
			} else if (value instanceof Object) {
				for (subName in value) {
					subValue = value[subName];
					if(subValue==0 || subValue){
						fullSubName = name + '.' + subName;
						innerObj = {};
						innerObj[fullSubName] = subValue;
						query += this._parseBody(innerObj) + '&';
					}
				
				}
			} else if (value !== undefined && value !== null)
				query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
		}
		
		return query.length ? query.substr(0, query.length - 1) : query;
	}
};


//模块
@NgModule({
	imports:[BrowserModule, HttpModule, FormsModule, DirectivesM, appRoutes],
	declarations:[
		AppComp,
		LoginComp,
		MenuComp,
		ContentComp,
		IndexSettingComp,
		//路由组件
		DataComp,
		OrderComp,
		UserComp,
		SettingComp,
		PayComp,
		BindWxComp,
		BoxComp,
		BoxSettingIndexComp, BoxAccountComp, BoxAddressComp, BoxBaseComp, BoxNoticeComp, BoxShipComp, BoxTimeComp, //盒子设置路由
		BoxCategoryComp, BoxProductComp, BoxProductIndexComp //盒子商品
	],
	bootstrap:[AppComp],
	providers:[NgLayer, XHRBackend, [{provide: Http, useClass:Ajax}], Notify]
})
class AppModule{}
platformBrowserDynamic().bootstrapModule(AppModule);